#include<cstdio>
struct Tree {
	int fa,ch[2],key,cnt,siz;
}tree[1000010];
int tot,root;
void pushup(int x)
{
	if(x) {
		tree[x].siz=tree[x].cnt;
		if(tree[x].ch[0]) tree[x].siz+=tree[tree[x].ch[0]].siz;
		if(tree[x].ch[1]) tree[x].siz+=tree[tree[x].ch[1]].siz;
	}
	return;
}
void rotate(int x)
{
	int fa=tree[x].fa,gf=tree[fa].fa,k=tree[fa].ch[1]==x;
	tree[fa].ch[k]=tree[x].ch[k^1],tree[tree[x].ch[k^1]].fa=fa;
	tree[x].ch[k^1]=fa,tree[fa].fa=x;
	if(gf) tree[gf].ch[tree[gf].ch[1]==fa]=x;
	tree[x].fa=gf;
	pushup(fa);pushup(x);
}
void splay(int x)
{
	int fa=tree[x].fa,gf=tree[fa].fa;
	while(fa) {
		if(gf) {
			if((tree[gf].ch[1]==fa)^(tree[fa].ch[1]==x)) rotate(x);
			else rotate(fa);
		}
		rotate(x);
		fa=tree[x].fa;
		gf=tree[fa].fa;
	}
	root=x;
	return ; 
}
void insert(int x)
{
	if(!root) {
		root=++tot;
		tree[tot].fa=tree[tot].ch[0]=tree[tot].ch[1]=0;
		tree[tot].cnt=tree[tot].siz=1;
		tree[tot].key=x;
		return ;
	}
	int now=root,last=0;
	do {
		if(tree[now].key==x) {
			tree[now].cnt++;
			pushup(now);
			pushup(last);
			splay(now); 
			return ;
		}
		last=now;
		now=tree[last].ch[x>tree[last].key];
		if(!now) {
			tree[last].ch[x>tree[last].key]=++tot;
			tree[tot].fa=last;
			tree[tot].ch[0]=tree[tot].ch[1]=0;
			tree[tot].cnt=tree[tot].siz=1;
			tree[tot].key=x;
			pushup(last);
			splay(tot);
			return ;
		}
	} while(true) ;
}
int find(int x)
{
	int ans=1,now=root;
	while(now) {
		if(x<tree[now].key) now=tree[now].ch[0];
		else {
			if(tree[now].ch[0]) ans+=tree[tree[now].ch[0]].siz;
			if(x==tree[now].key) {
				splay(now);
				return ans;
			}
			ans+=tree[now].cnt;
			now=tree[now].ch[1];
		}
	}
	return ans;
}
int findkth(int x)
{
	int now=root;
	do {
		if(tree[now].ch[0]&&x<=tree[tree[now].ch[0]].siz)
			now=tree[now].ch[0];
		else {
			if(tree[now].ch[0]) x-=tree[tree[now].ch[0]].siz;
			if(x<=tree[now].cnt) {
				splay(now);
				return now;
			}
			x-=tree[now].cnt;
			now=tree[now].ch[1];
		}
	} while(true) ;
	return 0;
}
int pre()
{
	int now=tree[root].ch[0];
	while(tree[now].ch[1]) now=tree[now].ch[1];
	return now;
}
int nxt()
{
	int now=tree[root].ch[1];
	while(tree[now].ch[0]) now=tree[now].ch[0];
	return now;
}
void del(int x)
{
	find(x);
	if(tree[root].cnt>1) {
		tree[root].cnt--;
		return ;
	}
	if(!tree[root].ch[0]&&!tree[root].ch[1]) {
		root=0;
		return ;
	}
	if(!tree[root].ch[0]) {
		root=tree[root].ch[1];
		tree[root].fa=0;
		return ;
	}
	if(!tree[root].ch[1]) {
		root=tree[root].ch[0];
		tree[root].fa=0;
		return ;
	}
	int lst=root;
	splay(pre());
	tree[root].ch[1]=tree[lst].ch[1];
	tree[tree[lst].ch[1]].fa=root;
	pushup(root);
	return ;
}
int main()
{
	int n;
	scanf("%d",&n);
	while(n--) {
		int opt,x;
		scanf("%d%d",&opt,&x);
		switch(opt) {
			case 1 : insert(x); break;
			case 2 : del(x); break;
			case 3 : printf("%d\n",find(x)); break;
			case 4 : printf("%d\n",tree[findkth(x)].key); break;
			case 5 : insert(x); printf("%d\n",tree[pre()].key); del(x); break;
			case 6 : insert(x); printf("%d\n",tree[nxt()].key); del(x); break;
		}
	}
}